<?php defined('SYSPATH') or die('No direct script access.');

/**
 * Define the start time of the application, used for profiling.
 */
if ( ! defined('KOHANA_START_TIME'))
{
    define('KOHANA_START_TIME', microtime(TRUE));
}

/**
 * Define the memory usage at the start of the application, used for profiling.
 */
if ( ! defined('KOHANA_START_MEMORY'))
{
    define('KOHANA_START_MEMORY', memory_get_usage());
}

// Load the base, low-level functions
require SYSPATH.'base'.EXT;

// Load the core Kohana class
require SYSPATH.'classes/kohana/core'.EXT;

if (is_file(APPPATH.'classes/kohana'.EXT))
{
    // Application extends the core
    require APPPATH.'classes/kohana'.EXT;
}
else
{
    // Load empty core extension
    require SYSPATH.'classes/kohana'.EXT;
}

/**
 * Set the default time zone.
 *
 * @see  http://kohanaframework.org/guide/using.configuration
 * @see  http://php.net/timezones
 */
date_default_timezone_set('Asia/Chongqing');

/**
 * Set the default locale.
 *
 * @see  http://kohanaframework.org/guide/using.configuration
 * @see  http://php.net/setlocale
 */
setlocale(LC_ALL, 'zh_cn.utf-8');

/**
 * Enable the Kohana auto-loader.
 *
 * @see  http://kohanaframework.org/guide/using.autoloading
 * @see  http://php.net/spl_autoload_register
 */
spl_autoload_register(array('Kohana', 'auto_load'));

/**
 * Enable the Kohana auto-loader for unserialization.
 *
 * @see  http://php.net/spl_autoload_call
 * @see  http://php.net/manual/var.configuration.php#unserialize-callback-func
 */
ini_set('unserialize_callback_func', 'spl_autoload_call');

// -- Configuration and initialization -----------------------------------------

/**
 * Set the default language
 */
I18n::lang('zh');

/**
 * Set Kohana::$environment if a 'KOHANA_ENV' environment variable has been supplied.
 *
 * Note: If you supply an invalid environment name, a PHP warning will be thrown
 * saying "Couldn't find constant Kohana::<INVALID_ENV_NAME>"
 */
if (isset($_SERVER['KOHANA_ENV']))
{
    Kohana::$environment = constant('Kohana::'.strtoupper($_SERVER['KOHANA_ENV']));
}

/**
 * Enable modules. Modules are referenced by a relative or absolute path.
 */
Kohana::modules(array(
    'script'     => APPPATH, // Application common library
    '360hqb'     => LIBPATH, // Application common library
    'cache'      => MODPATH.'cache', // Caching with multiple backends
    'database'   => MODPATH.'database', // Database access
));

/**
 * 初始化 CLI 模式
 */
CLI::init();

/**
 * 设置 Log 实例
 */
Kohana::$log->attach(new Kohana_Log_File(TMPPATH.'logs/cli'));

/**
 * 设置 Config 实例
 */
Kohana::$config->attach(new Config_File(BASEPATH.'config'));

/**
 * 默认的数据库名
 */
Database::$default = 'default';

/**
 * 缓存控制器
 */
Cache::$default = 'memcache';

/**
 * 运行 CLI 模式
 */
CLI::run($argv);

/**
 * Linux/Unix 输出换行
 */
if ( ! Kohana::$is_windows)
{
    echo "\n";
}
?>